extends Control

@onready var back_btn : Button = $ColorRect/MarginContainer/VBoxContainer/HBoxContainer/BackBtn
@onready var category_name_le : LineEdit = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/HBoxContainer/fields/CategoryNameLE
@onready var dec_number_le : LineEdit = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/HBoxContainer/fields/DecimalNumberLE
@onready var descr_te : TextEdit = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/HBoxContainer/fields/TextEdit
@onready var save_btn : Button = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/Button
@onready var parents_cat_ids_list : ItemList = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/HBoxContainer/fields/ParentCatIds

var sellected_parent_cat_id : int = -1

func _ready():	
	for cat in DbAdapter.get_all_categories_decimal_id_list():
		if cat.has("decimal_id") && cat["decimal_id"]:
			parents_cat_ids_list.add_item(str(cat["decimal_id"]))
	
	parents_cat_ids_list.item_selected.connect(_set_sellected_parent_cat_id)
	back_btn.pressed.connect(_go_to_view_categories_page)
	dec_number_le.text_changed.connect(_on_dec_num_edit)
	save_btn.pressed.connect(_on_save_changes)

func _set_sellected_parent_cat_id(id:int):
	sellected_parent_cat_id = DbAdapter.get_category_id_by_decimal_number(int(parents_cat_ids_list.get_item_text(id)))
	print("DEBB:: sellected parent cat id: ", id, " is: ", parents_cat_ids_list.get_item_text(id), " var is:", sellected_parent_cat_id)

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		_go_to_view_categories_page()

func _go_to_view_categories_page():
	get_tree().change_scene_to_file("res://view_catalog/view_catalog.tscn")

func _on_dec_num_edit(input:String):
	var regexp := RegEx.new()
	regexp.compile("^[0-9]{1,2}$")
	if not regexp.search(input):
		dec_number_le.delete_char_at_caret()

		#alias TEXT UNIQUE NOT NULL,
		#parent_type_id INTEGER, 
		#description TEXT,

func _on_save_changes():
	var result : Dictionary = {}
	if category_name_le.text.length() > 3:
		result["alias"] = category_name_le.text
	else:
		return _set_incorrect_value("название категории")
	if not dec_number_le.text.is_empty():
		result["decimal_id"] = int(dec_number_le.text)
	else:
		return _set_incorrect_value("децимальный номер категории")
	
	result["parent_type_id"] = sellected_parent_cat_id
	result["description"] = descr_te.text
	
	if not DbAdapter.append_category(result):
		print("ERR:: category is not append: ", result)
	_go_to_view_categories_page()


func _set_incorrect_value(who : String):
	var msg_lbl : Label = $ColorRect/MarginContainer/VBoxContainer/ColorRect/CenterContainer/VBoxContainer/IncorrectValueLbl
	
	msg_lbl.text = str("НЕКОРРЕКТНОЕ ЗНАЧЕНИЕ: ", who)
	var dead_timer := Timer.new()
	dead_timer.wait_time = 2
	dead_timer.timeout.connect(func():
		msg_lbl.text = " "
		msg_lbl.remove_child(dead_timer)
		dead_timer.queue_free())
	msg_lbl.add_child(dead_timer)
	dead_timer.start()
	return false
